/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.content.model.resp.*;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import top.continew.admin.content.mapper.ContentMapper;
import top.continew.admin.content.model.entity.ContentDO;
import top.continew.admin.content.model.query.ContentQuery;
import top.continew.admin.content.model.req.ContentReq;
import top.continew.admin.content.service.ContentService;

import java.util.*;

/**
 * 内容业务实现
 *
 * @author floyd
 * @since 2024/11/15 14:55
 */
@Service
@RequiredArgsConstructor
public class ContentServiceImpl extends BaseServiceImpl<ContentMapper, ContentDO, ContentResp, ContentDetailResp, ContentQuery, ContentReq> implements ContentService {

    @Override
    public PageResp<ContentResp> page(ContentQuery query, PageQuery pageQuery) {
        QueryWrapper<ContentDO> queryWrapper = this.buildQueryWrapper(query);
        super.sort(queryWrapper, pageQuery);
        IPage<ContentResp> page = baseMapper.selectContentPage(new Page<>(pageQuery.getPage(), pageQuery
            .getSize()), queryWrapper);
        PageResp<ContentResp> pageResp = PageResp.build(page, super.getListClass());
        pageResp.getList().forEach(this::fill);
        return pageResp;
    }

    @Override
    public List<EventStatisticsResp> eventStatisticsResp() {

        //构建一个YearStatisticsResp对象
        List<EventStatisticsResp> yearList = baseMapper.yearStatistics();

        //构建一个MonthStatisticsResp对象
        List<MonthStatisticsResp> monthList = baseMapper.monthStatistics();

        //构建一个EventTitleResp对象
        List<EventTitleResp> titleList = baseMapper.titleStatistics();

        //往yearList对象中拼接数据
        yearList.forEach(yearResp -> {

            List<StandardMonthResp> monthRespList = new ArrayList<>();

            monthList.stream()
                .filter(monthResp -> Objects.equals(monthResp.getYearNumber(), yearResp.getEventYear()))
                .forEach(monthResp -> {

                    StandardMonthResp monthObj = new StandardMonthResp();
                    monthObj.setEventMonth(monthResp.getEventMonth());

                    List<StandardTitleResp> titleRespList = new ArrayList<>();
                    titleList.stream()
                        .filter(titleResp -> Objects.equals(titleResp.getMonthStr(), monthResp.getEventMonth()))
                        .forEach(titleResp -> {

                            StandardTitleResp titleObj = new StandardTitleResp();
                            titleObj.setTitle(titleResp.getTitle());

                            titleRespList.add(titleObj);
                        });

                    monthObj.setMonthData(titleRespList);

                    monthRespList.add(monthObj);
                });

            //给YearData赋值
            yearResp.setYearData(monthRespList);
        });

        return yearList;
    }
}